#include "StdAfx.h"
#include <GlobalConfig.h>

const string PRESETS_DIR = "./resources/Missiles/";
const string PRESETS_PATH = "./resources/Missiles/Presets.dat";

unsigned int				DataManager::_currSituationIndex = INT_MAX;
vector<Situation>			DataManager::_presets;
vector<Address>				DataManager::_addrs;
vector<EnemyMissileInfo>	DataManager::_enemyMissiles;
vector<OurMissileInfo>		DataManager::_ourMissiles;

void DataManager::loadData()
{

	// todo : load from database
}

unsigned int DataManager::getPresetCount()
{
	return _presets.size();
}

Situation* DataManager::getPreset(unsigned int index)
{
	if (_presets.size() == 0) { return NULL; }

	if (index >= _presets.size())
	{
		index = _presets.size() - 1;
	}

	return &_presets[index];
}

void DataManager::newPresetAndMakeCurrent()
{
	Situation s;
	_presets.push_back(s);
	_currSituationIndex = _presets.size() - 1;
}

void DataManager::delPreset(unsigned int index)
{
	if (index >= _presets.size()) { return; }
	_presets.erase(_presets.begin()+index);
}

vector<int> DataManager::findPresets( const Situation& cond )
{
	// todo : query database
	vector<int> res;
	return res;
}

unsigned int DataManager::getAddressCount()
{
	return _addrs.size();
}

Address* DataManager::getAddress( unsigned int index )
{
	if (index >= _addrs.size()) { return NULL; }
	return &_addrs[index];
}

unsigned int DataManager::newAddress()
{
	Address a;
	_addrs.push_back(a);
	return _addrs.size()-1;
}

void DataManager::delAddress( unsigned int index )
{
	if (index >= _addrs.size()) { return; }
	_addrs.erase(_addrs.begin() + index);
}

unsigned int DataManager::getEnemyMissileCount()
{
	return _enemyMissiles.size();
}

EnemyMissileInfo* DataManager::getEnemyMissile( unsigned int index )
{
	if (index >= _enemyMissiles.size()) { return NULL; }
	return &_enemyMissiles[index];
}

unsigned int DataManager::newEnemyMissile()
{
	EnemyMissileInfo m;
	_enemyMissiles.push_back(m);
	return _enemyMissiles.size() - 1;
}

void DataManager::delEnemyMissile( unsigned int index )
{
	if (index >= _enemyMissiles.size())
	{
		return;
	}

	_enemyMissiles.erase(_enemyMissiles.begin() + index);
}

unsigned int DataManager::getOurMissileCount()
{
	return _ourMissiles.size();
}

OurMissileInfo* DataManager::getOurMissile( unsigned int index )
{
	if (index >= _ourMissiles.size()) { return NULL; }
	return &_ourMissiles[index];
}

unsigned int DataManager::newOurMissile()
{
	OurMissileInfo m;
	_ourMissiles.push_back(m);
	return _ourMissiles.size() - 1;
}

void DataManager::delOurMissile( unsigned int index )
{
	if (index >= _ourMissiles.size())
	{
		return;
	}

	_ourMissiles.erase(_ourMissiles.begin() + index);
}

void DataManager::saveData()
{

	// todo : save to database
}

Situation* DataManager::getCurrSituation()
{
	if (_currSituationIndex >= _presets.size()) { return NULL; }
	return &_presets[_currSituationIndex];
}

void DataManager::setCurrSituation(unsigned int indexInPresets)
{
	if (indexInPresets >= _presets.size())
	{
		indexInPresets = _presets.size() - 1;
	}
	_currSituationIndex = indexInPresets;
}

unsigned int DataManager::findAddressIndex(Address* addr)
{
	for (unsigned int i = 0; i < _addrs.size(); ++i)
	{
		if (&_addrs[i] == addr)
		{
			return i;
		}
	}
	return INT_MAX;
}

unsigned int DataManager::findOurMissileIndex(OurMissileInfo* missile)
{
	for (unsigned int i = 0; i < _ourMissiles.size(); ++i)
	{
		if (&_ourMissiles[i] == missile)
		{
			return i;
		}
	}
	return INT_MAX;
}

unsigned int DataManager::findEnemyMissileIndex(EnemyMissileInfo* missile)
{
	for (unsigned int i = 0; i < _enemyMissiles.size(); ++i)
	{
		if (&_enemyMissiles[i] == missile)
		{
			return i;
		}
	}
	return INT_MAX;
}